﻿using System;
using System.Collections.Generic;
using NHibernate;
using SimpleScrum.Core.Entities;
using SimpleScrum.Helpers;
using SimpleScrum.Utils;

namespace SimpleScrum.Areas.Scrum.Models
{
    public class ProjectModel
    {
        //private readonly Organization _currentOrganization = new OrganizationModel().GetOrganizationById(Client.CurrentOrganizationId);

        private ISession _session;

        public IList<Project> GetProjects(Guid id)
        {
            IList<Project> projects;
            using (_session = NHibernateHelper.GetSession())
            {
                //NHibernate query 
                IQuery query =
                    _session.CreateQuery("from Project where OrganizationId='" + id + "';");
                projects = query.List<Project>();
            }
            return projects;
        }
        public IList<Project> GetProjects()
        {
            IList<Project> projects;
            using (_session = NHibernateHelper.GetSession())
            {
                //NHibernate query 
                IQuery query =
                    _session.CreateQuery("from Project where OrganizationId='" + Client.Instance.CurrentOrganizationId + "';");
                projects = query.List<Project>();
            }
            return projects;
        }

        public Project GetProjectById(Guid id)
        {
            Project project;
            using (_session = NHibernateHelper.GetSession())
            {
                using (ITransaction tran = _session.BeginTransaction())
                {
                    project = _session.Get<Project>(id);
                    tran.Commit();
                }
            }
            return project;
        }

        public bool NewProject(Project project)
        {
            using (_session = NHibernateHelper.GetSession())
            {
                using (ITransaction tran = _session.BeginTransaction())
                {
                    _session.Save(project);
                    tran.Commit();
                }
            }
            return true;
        }

        public void UpdateProject(Project project)
        {
            using (_session = NHibernateHelper.GetSession())
            {
                using (ITransaction tran = _session.BeginTransaction())
                {
                    _session.Update(project);
                    tran.Commit();
                }
            }
        }

        public void DeleteProject(Project project)
        {
            using (_session = NHibernateHelper.GetSession())
            {
                using (ITransaction tran = _session.BeginTransaction())
                {
                    try
                    {
                        _session.Delete(project);
                        tran.Commit();
                    }
                    catch (Exception ex)
                    {
                        string msg = ex.Message;
                    }
                }
            }
        }
    }
}